package com.other.printer;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.orangelife.R;

import java.io.IOException;

public class PrinterActivity extends AppCompatActivity {

    private EscPosUtil pos;
    private String ip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_printer);
        findViewById(R.id.btnPrint).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                print();
            }
        });
    }

    private void printText() {
        ip = "192.168.0.10";
        try {
            pos = new EscPosUtil(ip, 9100, "GBK");
            pos.initPos();
            pos.printLine();
//            printData();
            //切纸
//            pos.feedAndCut();
            pos.printZz();
            pos.closeIOAndSocket();
            pos = null;
        } catch (IOException e) {
            Log.e(PrinterActivity.class.getSimpleName(), "链接打印机失败");
            e.printStackTrace();
        }
    }

    private void printData() throws IOException {
//        pos.bold(true);
//        pos.printTabSpace(2);
//        pos.printWordSpace(1);
        pos.printText("**测试店铺");

        pos.printLocation(0, 0);
        pos.printTextNewLine("----------------------------------------------");
//        pos.bold(false);
        pos.printTextNewLine("订 单 号：1005199");
        pos.printTextNewLine("用 户 名：15712937281");
        pos.printTextNewLine("桌    号：3号桌");
        pos.printTextNewLine("订单状态：订单已确认");
        pos.printTextNewLine("订单日期：2016/2/19 12:34:53");
        pos.printTextNewLine("付 款 人：线下支付（服务员：宝哥）");
        pos.printTextNewLine("服 务 员：1001");
    }

    private void print() {
        Toast.makeText(this, "打印", Toast.LENGTH_SHORT).show();
        new Thread(new Runnable() {
            @Override
            public void run() {
                printText();
            }
        }).start();
    }
}
